//
//  WolfEntityView.m
//  SGF
//
//  Created by shanshan on 10-6-10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WolfEntityView.h"
#import "WeaponEntityView.h"
#import "SSGame.h"

#import "GameSoundManager.h"

@interface WolfEntityView (Private)
-(void)initCastBalloonGreen;
-(void)initCastBalloonRed;
-(void)initCastBalloonYellow;
-(void)initDeath;
-(void)initJump;
-(void)initRun;
-(void)initStack;
-(void)initStoneGreen;
-(void)initStoneRed;
-(void)initStoneYellow;
@end


@implementation WolfEntityView


#pragma mark -
#pragma mark init

-(id)initWithBalloonColor:(NSString *)color{
	CMLog(@"in WolfEntityView init");
	if( (self=[super init]) ) {
		[self initCastBalloonGreen];
		[self initCastBalloonRed];
		[self initCastBalloonYellow];
		[self initDeath];
		[self initJump];
		[self initRun];
		[self initStack];
		[self initStoneGreen];
		[self initStoneRed];
		[self initStoneYellow];
		ballonColor = color;
		//Set the display frame of your sprite using idle animation
		[self setDisplayFrame:@"run" index:0]; //display idle1.png
		//	[mySprite setDisplayFrame:@"idle" index:1]; //display idle2.png
	}
	
	return self;
}

-(id)init{
	CMLog(@"in WolfEntityView init");
	if( (self=[super init]) ) {
		[self initWithBalloonColor:@"red"];
	}
	
	return self;
}


#pragma mark init Animations
-(void)initCastBalloonGreen{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"green" delay:0];
	[animation addFrameWithFilename:@"wolf_cast_balloon1.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon2.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon3.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon4_green.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initCastBalloonRed{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"red" delay:0];
	[animation addFrameWithFilename:@"wolf_cast_balloon1.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon2.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon3.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon4_red.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initCastBalloonYellow{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"yellow" delay:0];
	[animation addFrameWithFilename:@"wolf_cast_balloon1.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon2.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon3.png"];
	[animation addFrameWithFilename:@"wolf_cast_balloon4_yellow.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initDeath{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"death" delay:2];
	[animation addFrameWithFilename:@"wolf_death1.png"];
	[animation addFrameWithFilename:@"wolf_death2.png"];
	[animation addFrameWithFilename:@"wolf_death3.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initJump{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"jump" delay:0];
	[animation addFrameWithFilename:@"wolfa_jump1.png"];
	[animation addFrameWithFilename:@"wolfa_jump2.png"];
	[animation addFrameWithFilename:@"wolfa_jump3.png"];
	[animation addFrameWithFilename:@"wolfa_jump4.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initRun{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"run" delay:0];
	[animation addFrameWithFilename:@"wolfb_run1.png"];
	[animation addFrameWithFilename:@"wolfb_run2.png"];
	[animation addFrameWithFilename:@"wolfb_run3.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initStack{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"stack" delay:0];
	[animation addFrameWithFilename:@"wolf_stack1.png"];
	[animation addFrameWithFilename:@"wolf_stack2.png"];
	[animation addFrameWithFilename:@"wolf_stack3.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initStoneGreen{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"stoneGreen" delay:0];
	[animation addFrameWithFilename:@"wolf_stone1_green.png"];
	[animation addFrameWithFilename:@"wolf_stone2_green.png"];
	[animation addFrameWithFilename:@"wolf_stone3_green.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initStoneRed{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"stoneRed" delay:1];
	[animation addFrameWithFilename:@"wolf_stone1_red.png"];
	[animation addFrameWithFilename:@"wolf_stone2_red.png"];
	[animation addFrameWithFilename:@"wolf_stone3_red.png"];
	[self addAnimation:animation];
	[animation release];
}

-(void)initStoneYellow{
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"stoneYellow" delay:0];
	[animation addFrameWithFilename:@"wolf_stone1_yellow.png"];
	[animation addFrameWithFilename:@"wolf_stone2_yellow.png"];
	[animation addFrameWithFilename:@"wolf_stone3_yellow.png"];
	[self addAnimation:animation];
	[animation release];
}


#pragma mark Actions

-(void)enemyStack:(id)sender data:(SSGame *)game{
	CMLog(@"in enemyStack: %d", [game escapedMonsters].count);
	[self runAction:[CCMoveBy actionWithDuration:0 position:ccp(0, 28 * [game escapedMonsters].count)]];
	[[game escapedMonsters] addObject:self];
	
	[game updateNewPostionForPlayer];

	if ([SSStorage getSound]) {
		[[GameSoundManager sharedManager].soundEngine playEffect:@"wolf_climb.wav" pitch:1.0f pan:0.0f gain:1.0f];
	}
	//[self setPosition:CGPointMake(self.position.x, self.position.y - 30 * escapedMonsters.count)];
}

-(void)fireToPosition:(id)sender data:(SSGame *)game{
	WeaponEntityView *weapon = [[WeaponEntityView alloc] init];
	//weapon.position = ccp(self.position.x, self.position.y);
	weapon.position = ccp(self.position.x, self.position.y);
	[game addChild:weapon];
	[weapon fireStoneToPosition:[[game playerView] position]];
	
	[[game monsterProjectiles] addObject:weapon];
}

-(void)run:(SSGame *)game{
	CMLog(@"in runWithescapedMonsters");
	CCSpawn *runSpawn = [CCSpawn actions: [CCMoveBy actionWithDuration:2.0f position:ccp(100,0)], [CCRepeat actionWithAction: 
																								   [CCAnimate actionWithDuration:0.2f animation:[self animationByName:@"run"] restoreOriginalFrame:YES] times:10], nil];
	CCSpawn *jumpUpSpawn2 = [CCSpawn actions: [CCJumpBy actionWithDuration:1 position:ccp(10,0) height:20 jumps:1], 
							 [CCRepeat actionWithAction: [CCAnimate actionWithDuration:1 animation:[self animationByName:@"jump"] restoreOriginalFrame:YES] times:1], 
							 [CCMoveBy actionWithDuration:1.0f position:ccp(50,20)],
							 nil];
	
	id jumpUpSpawn = [CCEaseOut actionWithAction:jumpUpSpawn2 rate:2];
	CCSpawn *downSpawn = [CCSpawn actions:  
						  [CCMoveBy actionWithDuration:2.0f position:ccp(50,-60)],
						  nil];
	
	CCDelayTime *delay = [CCDelayTime actionWithDuration:2]; //TODO have stone, random delay
	id stone = [CCAnimate actionWithDuration:1.0f animation:[self animationByName:@"stoneRed"] restoreOriginalFrame:NO];
	CCCallFunc *fire = [CCCallFuncND actionWithTarget:self selector:@selector(fireToPosition:data:) data:game];//[[game playerView] position]];
	
	CCSequence *waffe = [CCSequence actions: delay, stone, fire, nil];
	
	CCSpawn *ballonSpawn = [CCSpawn actions:  
							[CCAnimate actionWithDuration:1.0f animation:[self animationByName: ballonColor] restoreOriginalFrame:NO],waffe, 
							[CCMoveBy actionWithDuration:5.0f position:ccp(-30,-210)],
							nil];
	
	
	CCCallFunc *callFunc = [CCCallFuncND actionWithTarget:self selector:@selector(enemyStack:data:) data: game];

	CCSequence *sequence = [CCSequence actions:runSpawn, jumpUpSpawn, downSpawn, ballonSpawn, runSpawn, runSpawn,
							[CCAnimate actionWithDuration:0.1f animation:[self animationByName:@"stack"] restoreOriginalFrame:NO], callFunc,
							nil];

	[self runAction:sequence];
}


@end
